<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 */

class Cli_referersip {
	public function run($args) {
		
		set_time_limit ( 0 );
		
		while (true)
		{
			$online = array();
			$call = array();
			 $data = Helper_Freeswitch::execute('show registrations');
			$result = array();
			if(preg_match_all('/sip:(\d.*)@/', $data, $result))
			{
				$online = implode("','", $result[1]);
				//echo ;die;
				QDB::getConn('fscall')->execute("DELETE FROM fscall.sip_online WHERE sipuser not in('{$online}')");
				//Helper_Common::log("DELETE FROM fscall.sip_online WHERE sipuser not in('{$online}')",'ss');
				//QDB::getConn('fscall')->execute('UPDATE fscall.sip_online set status = 2 WHEHRE');
			} 
			$data = Helper_Freeswitch::execute('show channels');
			$result1 = array();
			//internal/18685798168@
			if(preg_match_all('/internal.*\/([0-9]{11})\@/', $data, $result1))
			{
				$call = implode("','", $result1[1]);
				QDB::getConn('fscall')->execute("UPDATE fscall.sip_online set status=3,lasttime=now() WHERE sipuser  in('{$call}')");
			
			}
			//$calling = array_intersect($online, $call);
			//dump($calling);
			sleep(5);
		}
		
		
		
	}
		
}